package cn.edu.cug.cs.gtl.plugins.hello.actions;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

public class HelloMessageAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("hello-resource", Locale.getDefault());
        name=resourceBundle.getString("menu.help.hello");
        menuItem.setText(name);
        this.theApp = theApp;
        menuItem.addActionListener(this);
        theApp.getMainFrame().getHelpMenu().add(menuItem);
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e the event to be processed
     */
    @Override
    public void actionPerformed(ActionEvent e) {


        Configuration conf = new Configuration();


        //这里指定使用的是 hdfs文件系统
        conf.set("fs.defaultFS", "hdfs://124.71.34.100:8020/");

        FileSystem fileSystem = null;
        try {
            String hdfsPath="hdfs://124.71.34.100:8020/data/lake/"+"17181be8-c669-4db2-a312-814cb27f27d8.jpg";
            fileSystem = FileSystem.get(conf);
            boolean b = fileSystem.exists(fileSystem.getHomeDirectory());
            if(b){
                System.err.println(fileSystem.getHomeDirectory()+"HDFS路径已经存在");
                JDialog dialog = new JDialog(theApp.getMainFrame(), "HDFS路径已经存在", true);
                dialog.setSize(200, 100);
                dialog.setLocationRelativeTo(theApp.getMainFrame());
                dialog.setVisible(true);
            }
            else{
                System.err.println("HDFS路径不存在");
                JDialog dialog = new JDialog(theApp.getMainFrame(), "HDFS路径不存在", true);
                dialog.setSize(200, 100);
                dialog.setLocationRelativeTo(theApp.getMainFrame());
                dialog.setVisible(true);
            }
        } catch (IOException ex) {
            JDialog dialog = new JDialog(theApp.getMainFrame(), "Exception", true);
            dialog.setSize(200, 100);
            dialog.setLocationRelativeTo(theApp.getMainFrame());
            dialog.setVisible(true);
            throw new RuntimeException(ex);
        }






    }
}
